/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.softace.jdbc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * An object that can be used to get information about some basic properties of
 * the columns in a {@code ResultSet} object.
 *
 * @see JdbcManager#getMetaDataForResultSet(ResultSet)
 *
 * @author s1lent_warrior
 */
public class JdbcResultSetMetaData {

    private final int columnCount;
    private final int rowCount;
    private final List<String> tables;
    private final Map<String, JdbcColumn> columns;

    JdbcResultSetMetaData(int columnCount, int rowCount, Map<String, JdbcColumn> columns) {
        this.columnCount = columnCount;
        this.rowCount = rowCount;
        this.columns = columns;

        Set<String> unsortedTables = new TreeSet();
        for (Map.Entry<String, JdbcColumn> entry : columns.entrySet()) {
            JdbcColumn column = entry.getValue();
            unsortedTables.add(column.getTableName().toLowerCase());
        }
        this.tables = new ArrayList(unsortedTables);
    }

    /**
     * Retrieves number of columns in a {@code ResultSet}.
     *
     * @return the column count
     */
    public int getColumnCount() {
        return columnCount;
    }

    /**
     * Gets the number of rows in a {@code ResultSet}
     *
     * @return the row count
     */
    public int getRowCount() {
        return rowCount;
    }

    /**
     * Retrieves a {@link  java.util.List} of table names queried in a
     * {@code ResultSet}
     *
     * @return list of table names
     */
    public List<String> getTables() {
        return tables;
    }

    /**
     * Gets a {@link  java.util.Map} with {@code key=full column name} (i.e: in
     * {@code table_name.column_name} format) and {@code value = JdbcColumn}
     * object.
     *
     * @return a {@code java.util.Map} of {@link JdbcColumn}
     * @see JdbcColumn
     */
    public Map<String, JdbcColumn> getColumns() {
        return columns;
    }

    /**
     * Finds the {@link JdbcColumn} for the given column title.
     *
     * @param columnTitle The column title. <br>Note: {@code columnTitle}
     * should be in {@code table_name.column_name} format NOT
     * {@code table_alias.column_name}, OR {@code table_name.column_alias}, OR
     * {@code table_alias.column_alias}
     * @return the {@code JdbcColumn} or {@code null} if no column found
     */
    public JdbcColumn findColumnFromTitle(String columnTitle) {
        String[] split = columnTitle.split("\\.");

        for (Map.Entry<String, JdbcColumn> entry : columns.entrySet()) {
            JdbcColumn column = entry.getValue();

            if (split.length == 2) {
                if (this.tables.contains(split[0].toLowerCase())) {
                    String _columnTitle = column.getTableName() + "." + column.getColumnAlias();
                    if (columnTitle.equals(_columnTitle)) {
                        return column;
                    } else if (split[1].equals(column.getColumnAlias())) {
                        return column;
                    }
                } else {
                    return null;
                }
            } else {
                if (split[1].equals(column.getColumnTitle())) {
                    return column;
                } else {
                    return null;
                }
            }
        }
        return null;
    }
}
